<?php 
/*
Plugin Name: BH Widgets
Plugin URI: 
Description: Adds "Sidebar Widgets" panel under Presentation menu
Author: bh@blackhacker.net
Version: 0.6
Author URI: http://blackhacker.net
*/

/*
Copyright 2007,2008 Benjamin V King

BH Widgets is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

BH Widgets is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with BH Widgets; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

$bh_widgets_db_version="1.0";

function bh_widgets_install() {
	global $wpdb;
	global $bh_widgets_db_version;

	$table_name = $wpdb->prefix . "widgets";
	
	if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
      $sql = "CREATE TABLE IF NOT EXISTS ". $table_name ." (";
      $sql .= "`widget_id` bigint(20) NOT NULL auto_increment,";
      $sql .= "`widget_name` varchar(200) NOT NULL,";
      $sql .= "`widget_class` varchar(200) NOT NULL,";
      $sql .= "`widget_attributes` text,";
      $sql .= "`widget_delete` tinyint(1) NOT NULL default '0',";
      $sql .= "PRIMARY KEY  (`widget_id`),";
      $sql .= "UNIQUE KEY `widget_name` (`widget_name`),";
      $sql .= "KEY `widget_class` (`widget_class`),";
      $sql .= "KEY `delete` (`widget_delete`));";

      require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
      dbDelta($sql);
      update_option("bh_widgets_db_version", $bh_widgets_db_version);
 }
}

register_activation_hook(__FILE__,'bh_widgets_install');

include_once('BH_Text_Widget.php');
include_once('BH_Category_Dropdown_Widget.php');
include_once('BH_Tag_Dropdown_Widget.php');
include_once('BH_Pages_Widget.php');
include_once('BH_Search_Widget.php');
include_once('BH_Archive_Calendar_Widget.php');
include_once('BH_Links_Widget.php');
include_once('BH_Spacer_Widget.php');
include_once('BH_Recent_Entries_Widget.php');
include_once('BH_Recent_Comments_Widget.php');
include_once('BH_RSS_Widget.php');
include_once('BH_Post_Tag_Widget.php');
include_once('BH_Widget_Manager.php');
include_once('BH_Tag_Cloud_Widget.php');
include_once('BH_Iframe_Widget.php');
include_once('BH_Include_Widget.php');
include_once('BH_Clone_Widget.php');


//A recursive function to find and include user defined widgets
function include_widgets($dir) {
	if (!is_dir($dir))
		return false;
		
    if (!($dh = opendir($dir)))
    	return false;
    	
    $cnt = 0;
    	
    while (($file = readdir($dh)) !== false) {
    	if (($file == '.') || ($file == '..'))
    		continue;
        			        
        if (!is_readable($dir . $file))
        	continue;
        	
        if (is_dir($dir . $file)) {
        	$cnt += include_widgets($dir . $file);
        	continue;
        }

        if (!preg_match('/\.php$/', $file))
        	continue;
        	
        $cnt++;
        include_once($dir . $file);
	}
	
	return $cnt;
}

include_widgets(ABSPATH . PLUGINDIR . '/widgets/');

$bh_widget_mgr = new BH_Widget_Manager;
add_action('widgets_init', array($bh_widget_mgr, 'widgets_init'));
?>
